package project.product.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import kernel.web.Page;
import kernel.web.PageActionSupport;

import org.apache.struts2.ServletActionContext;

import project.order.OrderTemp;
import project.order.OrderTempService;
import project.order.weixi.SDKRuntimeException;
import project.order.weixi.WxPayHelper;
import project.order.work.WxOpenIdHelper;
import project.product.Brand;
import project.product.BrandService;
import project.product.ButtonService;
import project.product.Cloth;
import project.product.ClothService;
import project.product.StyleService;
import project.user.User;
import project.user.UserService;
import project.vip.po.Vip;
import project.vip.services.AdminVipService;
import project.vip.utils.KBProperties;
import project.vip.utils.VipWxOpenIdHelper;

public class StlyeAction extends PageActionSupport {

    private static final long serialVersionUID = -6700196000611774869L;
    // 用户姓名
    private String name;

    // 手机号码
    private String phone;

    // 收货地址
    private String address;

    private String cloth_id;

    private StyleService styleService;

    private OrderTempService orderTempService;

    private String openid;


    private UserService userService;

    private boolean call;

    private boolean trade_in;

    private String jsAPI;
    
    private String stlye_id;
    
    private String btn_id;

    private String code;

    private String height;

    private String weight;

    private String size;
    
    private String size2;

    private String positive;

    private String back;

    private String text;

    private String images1;

    private String images2;

    private String sign;
    
    private String redirect_url;

    private String fd4sm;
    private String fd4cs;
    private double bookPrice = 0.0;
    public String go() throws IOException {


        OrderTemp temp = new OrderTemp();
        temp.setCloth(cloth_id);
        temp.setCall(call);
        temp.setTrade_in(trade_in);
        temp.setStlye(stlye_id);
        temp.setButton(btn_id);
        temp.setHeight(height);
        temp.setWeight(weight);
        temp.setSize(size);
        temp.setPositive(positive);
        temp.setBack(back);
        temp.setText(text);
        temp.setImages1(images1);
        temp.setImages2(images2);
        temp.setSign(sign);
        temp.setSize2(size2);
        temp.setName(name);
        temp.setAddress(address);
        temp.setPhone(phone);
        if(null != fd4sm && fd4sm.equals("true")){
        	temp.setFd4sm("10");
        }else{
        	temp.setFd4sm("00");
        }
        if(null != fd4cs && fd4cs.equals("true")){
        	temp.setFd4cs("10");
        }else{
        	temp.setFd4cs("00");
        }
        
        orderTempService.save(temp);
        if (KBProperties.isTest) {
        	redirect_url = WxOpenIdHelper.getCodeUrl(temp.getId().toString());
        }else{
        	redirect_url = "http://127.0.0.1:8099/sjcf_root/public/orderAction!view.action?state="+temp.getId().toString();
        }
         

        return "next";

    }


    private File file1;

    private File file2;

    public String upImages1() {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        String newFileName = null;
        try {
            newFileName = UUID.randomUUID().toString() + "." + "png";

            // 建立文件输出流
            fos = new FileOutputStream(getSavePath() + "\\" + newFileName);
            // 建立文件上传流
            fis = new FileInputStream(file1);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(fos, fis);
        }
        return newFileName;
    }

    public String upImages2() {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        String newFileName = null;
        try {
            newFileName = UUID.randomUUID().toString() + "." + "png";
            fos = new FileOutputStream(getSavePath() + "\\" + newFileName);
            // 建立文件上传流
            fis = new FileInputStream(file2);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(fos, fis);
        }
        return newFileName;
    }

    /**
     * 返回上传文件的保存位置
     * 
     * @return
     */
    public String getSavePath() throws Exception {
        return ServletActionContext.getServletContext().getRealPath("userimages");
    }

    private void close(FileOutputStream fos, FileInputStream fis) {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                System.out.println("FileInputStream关闭失败");
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                System.out.println("FileOutputStream关闭失败");
                e.printStackTrace();
            }
        }
    }
    
    private ButtonService buttonService;
    private ClothService clothService;
    private BrandService brandService;
    protected Page page2;
    private Brand brandVip;
    private AdminVipService adminVipService;
	private String openId;
	private String state;
    
    public String viewWx() throws SDKRuntimeException {

		// 通过微信跳转到view
		String wxuri = KBProperties.sjcfHttp + "/public/stlyeAction!view.action";
		redirect_url = VipWxOpenIdHelper.getCodeUrl(cloth_id, wxuri);
//		redirect_url = "http://127.0.0.1:8099/sjcf_root/public/stlyeAction!view.action"+"?state="+cloth_id;
		return "viewWx";

	}
    public String view() {
    	HttpServletRequest req = (HttpServletRequest) ServletActionContext
				.getRequest();
		if (KBProperties.isTest) {
			openId = VipWxOpenIdHelper.getOpenId(code);
			if(null == openId){
				Object o = req.getSession().getAttribute("curOpenId");//获取session中的
				if(null != o){
					openId = o.toString();
				}
			}
			req.getSession().setAttribute("curOpenId", openId);//设置到session中
		}
		cloth_id = state;
       
		System.out.println("+++++++++openId"+openId);
        User userInfo = userService.find(openId);
        if (userInfo != null) {
            name = userInfo.getName();
            phone = userInfo.getPhone();
            address = userInfo.getAddress();
        }
        Vip vip = adminVipService.find(openId);
		if (null != vip) {
			
		}else{
			redirect_url = "http://www.mrshine.cn/sjcf_root/vip/vip_center00.jsp?p=show";
			return "order";
		}
    	Cloth clothEn = clothService.getCloth(cloth_id);
    	Brand brand = brandService.getBrand(clothEn.getBrand_id());
    	brandVip = brand;
    	//增加判断单品 逻辑
    	if(null != clothEn.getBrand_id()){
    		
    		bookPrice = brand.getBookPrice();
    		if(brand.getSingle()){
    			OrderTemp temp = new OrderTemp();
                temp.setBrand(brand.getId().toString());
                temp.setCloth(cloth_id);
                 orderTempService.save(temp);
                 if (KBProperties.isTest) {
                	 redirect_url = WxOpenIdHelper.getSingleCodeUrl(temp.getId().toString());
                 }else{
                	 redirect_url = "http://127.0.0.1:8099/sjcf_root/public/orderSingleAction!view.action?state="+temp.getId().toString();
                 }
                 
                 
                 return "order";
    		}
    	}
        page = Page.EMPTY_PAGE;
        //page.setElements(styleService.findByClothId(cloth_id));
        page.setElements(styleService.findByBrandId(clothEn.getBrand_id()));
        page2 = new Page();
        //page2.setElements(buttonService.findByClothId(cloth_id));
        page2.setElements(buttonService.findByBrandId(clothEn.getBrand_id()));
        super.page = page;
        this.tabs = this.bulidTabs();
        //获取js-sdk的config
        WxPayHelper wxPayHelper = new WxPayHelper();
        try {
			jsAPI = wxPayHelper.getJsAPI(req.getRequestURL()+"?"+req.getQueryString());
		} catch (SDKRuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
        return "view";
    }

    public String getCloth_id() {
        return cloth_id;
    }

    public void setCloth_id(String cloth_id) {
        this.cloth_id = cloth_id;
    }

    public void setStyleService(StyleService styleService) {
        this.styleService = styleService;
    }

    public boolean isCall() {
        return call;
    }

    public void setCall(boolean call) {
        this.call = call;
    }

    public boolean isTrade_in() {
        return trade_in;
    }

    public void setTrade_in(boolean trade_in) {
        this.trade_in = trade_in;
    }

    public String getStlye_id() {
        return stlye_id;
    }

    public void setStlye_id(String stlye_id) {
        this.stlye_id = stlye_id;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getSize() {
        return size;
    }

    public void setSize(String size) {
        this.size = size;
    }

    public String getPositive() {
        return positive;
    }

    public void setPositive(String positive) {
        this.positive = positive;
    }

    public String getBack() {
        return back;
    }

    public void setBack(String back) {
        this.back = back;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public File getFile1() {
        return file1;
    }

    public void setFile1(File file1) {
        this.file1 = file1;
    }

    public File getFile2() {
        return file2;
    }

    public void setFile2(File file2) {
        this.file2 = file2;
    }

    public String getImages1() {
        return images1;
    }

    public void setImages1(String images1) {
        this.images1 = images1;
    }

    public String getImages2() {
        return images2;
    }

    public void setImages2(String images2) {
        this.images2 = images2;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public void setOrderTempService(OrderTempService orderTempService) {
        this.orderTempService = orderTempService;
    }

    public String getRedirect_url() {
        return redirect_url;
    }

    public void setRedirect_url(String redirect_url) {
        this.redirect_url = redirect_url;
    }

    public String getSize2() {
        return size2;
    }

    public void setSize2(String size2) {
        this.size2 = size2;
    }

    public void setButtonService(ButtonService buttonService) {
        this.buttonService = buttonService;
    }

	public Page getPage2() {
		return page2;
	}

	public void setPage2(Page page2) {
		this.page2 = page2;
	}

	public String getBtn_id() {
		return btn_id;
	}

	public void setBtn_id(String btn_id) {
		this.btn_id = btn_id;
	}

	public void setClothService(ClothService clothService) {
		this.clothService = clothService;
	}

	public void setBrandService(BrandService brandService) {
		this.brandService = brandService;
	}

	public String getJsAPI() {
		return jsAPI;
	}

	public void setJsAPI(String jsAPI) {
		this.jsAPI = jsAPI;
	}

	public double getBookPrice() {
		return bookPrice;
	}

	public void setBookPrice(double bookPrice) {
		this.bookPrice = bookPrice;
	}

	/**
	 * @return the brandVip
	 */
	public Brand getBrandVip() {
		return brandVip;
	}

	/**
	 * @param brandVip the brandVip to set
	 */
	public void setBrandVip(Brand brandVip) {
		this.brandVip = brandVip;
	}

	/**
	 * @return the fd4sm
	 */
	public String getFd4sm() {
		return fd4sm;
	}

	/**
	 * @param fd4sm the fd4sm to set
	 */
	public void setFd4sm(String fd4sm) {
		this.fd4sm = fd4sm;
	}

	/**
	 * @return the fd4cs
	 */
	public String getFd4cs() {
		return fd4cs;
	}

	/**
	 * @param fd4cs the fd4cs to set
	 */
	public void setFd4cs(String fd4cs) {
		this.fd4cs = fd4cs;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}

	/**
	 * @param phone the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}

	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}

	/**
	 * @param address the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}

	/**
	 * @return the openid
	 */
	public String getOpenid() {
		return openid;
	}

	/**
	 * @param openid the openid to set
	 */
	public void setOpenid(String openid) {
		this.openid = openid;
	}

	/**
	 * @return the userService
	 */
	public UserService getUserService() {
		return userService;
	}

	/**
	 * @param userService the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * @return the state
	 */
	public String getState() {
		return state;
	}

	/**
	 * @param state the state to set
	 */
	public void setState(String state) {
		this.state = state;
	}

	/**
	 * @return the openId
	 */
	public String getOpenId() {
		return openId;
	}

	/**
	 * @param openId the openId to set
	 */
	public void setOpenId(String openId) {
		this.openId = openId;
	}

	/**
	 * @param adminVipService the adminVipService to set
	 */
	public void setAdminVipService(AdminVipService adminVipService) {
		this.adminVipService = adminVipService;
	}


    

}
